<?php
    $hide=$_REQUEST["hide"];

    
    if($hide=="addr"){// 获取地址信息
        $uname=$_REQUEST["uname"];
        $conn=mysqli_connect("127.0.0.1","root","","h52106");
        mysqli_query($conn,"SET NAMES utf8");
        $sql="SELECT * FROM addr";
        $rs=mysqli_query($conn,$sql);
        $arr=[];
        while(($row=mysqli_fetch_assoc($rs))!=null){
            array_push($arr,$row);
        }
        // echo $_GET["key"]."(".JSON_encode($arr).")";
        echo (JSON_encode($arr));
    }else if($hide=="addAddr"){// 新增地址
        $conn=mysqli_connect("127.0.0.1","root","","h52106");
        mysqli_query($conn,"SET NAMES utf8");
        $uname=$_REQUEST["uname"];
        $name=$_REQUEST["name"];
        $phone=$_REQUEST["phone"];
        $cho_Province=$_REQUEST["cho_Province"];
        $cho_City=$_REQUEST["cho_City"];
        $cho_Area=$_REQUEST["cho_Area"];
        $addr=$_REQUEST["addr"];
        $addr=$cho_Province.$cho_City.$cho_Area." ".$addr;//地址

        $sql="INSERT INTO addr VALUES(0,'$name','$phone','$addr','$uname')";
        $rs=mysqli_query($conn,$sql);
        if($rs){
            echo "<script>sessionStorage.state='新增成功';location='../html/jc_person_address.html'</script>";
        }else{
            echo "<script>sessionStorage.state='新增失败';location='../html/jc_person_address.html'</script>";
        }
    }else if($hide=="delAddr"){// 删除地址
        $conn=mysqli_connect("127.0.0.1","root","","h52106");
        mysqli_query($conn,"SET NAMES utf8");
        $id=$_GET["id"];
        $sql="DELETE FROM addr WHERE id=$id";
        $rs=mysqli_query($conn,$sql);
        if($rs){
            echo "1";
        }else{
            echo "";
        }
    }else if($hide=="getUser"){// 获取用户信息
        $conn=mysqli_connect("127.0.0.1","root","","h52106");
        mysqli_query($conn,"SET NAMES utf8");
        $sql="SELECT * FROM qquser";
        $rs=mysqli_query($conn,$sql);
        $arr=[];
        while(($row=mysqli_fetch_assoc($rs))!=null){
            array_push($arr,$row);
        }
        echo (JSON_encode($arr));
    }else if($hide=="getGood"){// 获取商品信息
        $conn=mysqli_connect("127.0.0.1","root","","h52106");
        mysqli_query($conn,"SET NAMES utf8");
        $sql="SELECT * FROM jcpgood";
        $rs=mysqli_query($conn,$sql);
        $arr=[];
        while(($row=mysqli_fetch_assoc($rs))!=null){
            array_push($arr,$row);
        }
        echo (JSON_encode($arr));
    }else if($hide=="addGood"){// 加入购物车数据
        $conn=mysqli_connect("127.0.0.1","root","","h52106");
        mysqli_query($conn,"SET NAMES utf8");
        $name=$_REQUEST["name"];
        $goodid=$_REQUEST["goodid"];
        $userid=$_REQUEST["userid"];
        $oldprice=$_REQUEST["oldprice"];
        $newprice=$_REQUEST["newprice"];
        $count=$_REQUEST["count"];
        $img=$_REQUEST["img"];

        $sql1="SELECT * FROM shopcar";
        $rs1=mysqli_query($conn,$sql1);

        while(($row=mysqli_fetch_assoc($rs1))!=null){
            if($row["goodsid"]==$goodid && $row["userid"]==$userid){
                $count=$count+$row["count"];
                $id = $row["id"];
                $upd="UPDATE shopcar SET count='$count' WHERE id='$id'";
                $updrs=mysqli_query($conn,$upd);
                return false;
            } 
        }

        $sql="INSERT INTO shopcar VALUES(0,'$name','$goodid','$userid','$oldprice','$newprice','$count','$img')";
        $rs=mysqli_query($conn,$sql);
        if($rs){
            echo "<script>sessionStorage.state='新增成功';location='../html/jc_details.html'</script>";
        }else{
            echo "<script>sessionStorage.state='新增失败';location='../html/jc_details.html'</script>";
        }
    }else if($hide=="getShopcar"){// 获取购物车id
        $conn=mysqli_connect("127.0.0.1","root","","h52106");
        mysqli_query($conn,"SET NAMES utf8");
        $sql="SELECT * FROM shopcar";
        $rs=mysqli_query($conn,$sql);
        $arr=[];
        while(($row=mysqli_fetch_assoc($rs))!=null){
            array_push($arr,$row);
        }
        echo (JSON_encode($arr));
    }else if($hide=="setcount"){// 计算商品数量
        $conn=mysqli_connect("127.0.0.1","root","","h52106");
        mysqli_query($conn,"SET NAMES utf8");

        $id=$_REQUEST["id"];
        $userid=$_REQUEST["userid"];
        $count=$_REQUEST["count"];
        
        $sql="SELECT * FROM shopcar";
        $rs=mysqli_query($conn,$sql);
        while(($row=mysqli_fetch_assoc($rs))!=null){
            if($row["id"]==$id && $row["userid"]==$userid){
                $upd="UPDATE shopcar SET count='$count' WHERE id=$id";
                $updrs=mysqli_query($conn,$upd);
                echo $count;
                return false;
            }
        }
    }else if($hide=="delshop"){// 删除购物车
        $conn=mysqli_connect("127.0.0.1","root","","h52106");
        mysqli_query($conn,"SET NAMES utf8");
        $id=$_REQUEST["id"];
        $del="DELETE FROM shopcar WHERE id=$id";
        $delrs=mysqli_query($conn,$del);
        if($delrs){
            echo "1";
        }else{
            echo "";
        }
    }else if($hide=="addcollect"){// 添加收藏
        $conn=mysqli_connect("127.0.0.1","root","","h52106");
        mysqli_query($conn,"SET NAMES utf8");
        $name=$_REQUEST["name"];
        $goodid=$_REQUEST["goodid"];
        $userid=$_REQUEST["userid"];
        $oldprice=$_REQUEST["oldprice"];
        $newprice=$_REQUEST["newprice"];
        $img=$_REQUEST["img"];

        mysqli_query($conn,"SET NAMES utf8");
        $sql="SELECT * FROM collect";
        $rs=mysqli_query($conn,$sql);
        
        $arr=[];
        while(($row=mysqli_fetch_assoc($rs))!=null){
            if($row["goodsid"]==$goodid){
                array_push($row,$arr);
                return false;
            }
        }
        echo $arr;

        $add="INSERT INTO collect VALUES(0,'$name','$goodid','$userid','$oldprice','$newprice','$img')";
        $addrs=mysqli_query($conn,$add);
        if($addrs){
            echo "<script>sessionStorage.addcollect='新增成功';location='../html/jc_person_collect.html'</script>";
        }else{
            echo "<script>sessionStorage.addcollect='新增失败';location='../html/jc_person_collect.html'</script>";
        }

    }else if($hide=="findcollect"){// 获取收藏
        $conn=mysqli_connect("127.0.0.1","root","","h52106");
        mysqli_query($conn,"SET NAMES utf8");
        $sql="SELECT * FROM collect";
        $rs=mysqli_query($conn,$sql);
        $arr=[];
        while(($row=mysqli_fetch_assoc($rs))!=null){
            array_push($arr,$row);
        }
        echo (JSON_encode($arr));
    }else if($hide==delcollect){ // 删除收藏
        $conn=mysqli_connect("127.0.0.1","root","","h52106");
        mysqli_query($conn,"SET NAMES utf8");
        $id=$_REQUEST["id"];
        $del="DELETE FROM collect WHERE id=$id";
        $delrs=mysqli_query($conn,$del);
        if($delrs){
            echo "1";
        }else{
            echo "";
        }
    }


?>